package com.ytt.core.api.controller.user;

import com.ytt.core.api.security.CurrentUser;
import com.ytt.core.model.common.PageDataDTO;
import com.ytt.core.model.common.SimpleDataDTO;
import com.ytt.core.model.user.UserDto;
import com.ytt.core.model.user.UserEditDTO;
import com.ytt.core.model.user.UserRegisterDTO;
import com.ytt.core.service.service.user.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * UserController
 *
 * @author chenwen
 * @date 2019/9/16 10:36
 */
@Api(tags = {"用户管理"})
@RestController
@RequestMapping("/api/authority/users")
@Validated
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }



//    @ApiOperation(value = "新建用户，设置普通用户角色懒得做用户角色分配了")
//    @PreAuthorize("hasAnyAuthority('USER_CREATE')")
//    @PostMapping(value = "")
//    public UserDto create(
//            @ApiParam(value = "用户信息", required = true) @RequestBody @Validated UserEditDTO userEditDTO) {
//        return userService.create(userEditDTO);
//    }
//
//    @ApiOperation(value = "注册用户")
//    @PostMapping(value = "/register")
//    public UserDto register(
//            @ApiParam(value = "用户信息", required = true) @RequestBody @Validated UserRegisterDTO userRegisterDTO) {
//        return userService.register(userRegisterDTO);
//    }

    @ApiOperation(value = "检查登录名唯一性,不需要权限")
    @GetMapping(value = "/account-only")
    public SimpleDataDTO<Boolean> checkAccountCode(
            @ApiParam(value = "登录名", required = true) @RequestParam(value = "account") String account,
            @ApiParam(value = "用户id") @RequestParam(value = "id", required = false) String id) {
       return userService.checkAccountAvailability(id,account);
    }

    @ApiOperation(value = "检查邮箱唯一性，不需要权限")
    @GetMapping(value = "/email-only")
    public SimpleDataDTO<Boolean> checkEmailCode(
            @ApiParam(value = "邮箱", required = true) @RequestParam(value = "email") String email,
            @ApiParam(value = "用户id") @RequestParam(value = "id", required = false) String id) {
        return userService.checkEmailAvailability(id,email);
    }

//    @ApiOperation(value = "修改用户")
//    @PreAuthorize("hasAnyAuthority('USER_UPDATE')")
//    @PutMapping(value = "/{id}")
//    public UserDto update(
//            @ApiParam(value = "用户", required = true) @PathVariable(name = "id") String id,
//            @ApiParam(value = "用户信息", required = true) @RequestBody @Validated UserEditDTO userEditDTO) {
//        return userService.update(id, userEditDTO);
//    }
//
//    @ApiOperation(value = "删除用户")
//    @PreAuthorize("hasAnyAuthority('USER_DELETE')")
//    @DeleteMapping(value = "/{id}")
//    public void delete(
//            @ApiParam(value = "用户ID", required = true) @PathVariable(name = "id") String id) {
//        userService.delete(id);
//    }


//    @ApiOperation(value = "查询用户登录账号是否可用")
//    @PreAuthorize("hasAnyAuthority('USER_UPDATE,USER_CREATE')")
//    @GetMapping(value = "/availability")
//    public SimpleDataDTO<Boolean> checkAccountAvailability(
//            @ApiParam(value = "用户ID, 新增用户时检查账号可用性可不填") @RequestParam(value = "id", required = false) String userId,
//            @ApiParam(value = "账号", required = true) @RequestParam(value = "account") String account) {
//        return userService.checkAccountAvailability(userId, account);
//    }

//
//    @ApiOperation(value = "分页查询用户")
//    @PreAuthorize("hasAnyAuthority('USER_RETRIEVE')")
//    @GetMapping(value = "", params = {"page", "size", "sort"})
//    public PageDataDTO<UserDto> findOnePage(
//            @ApiParam(value = "页号，从0开始", required = true, defaultValue = "0") @RequestParam("page") @Min(0) int page,
//            @ApiParam(value = "每页纪录条数", required = true, defaultValue = "20") @RequestParam("size") @Min(1) @Max(100) int size,
//            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false, defaultValue = "id,desc") String sort,
//            @ApiParam(value = "用户名,支持模糊查询") @RequestParam(value = "name", required = false) String name) {
//        return userService.findOnePage(page, size, sort, name,CurrentUser.getCurrentUser().getFilePath());
//    }
//
//    @ApiOperation(value = "根据ID查找用户，用户包含基本资料")
//    @PreAuthorize("hasAnyAuthority('USER_RETRIEVE')")
//    @GetMapping(value = "/{id}")
//    public UserDto findById(
//            @ApiParam(value = "用户ID", required = true) @PathVariable(name = "id") String id) {
//        return userService.findById(id);
//    }
}
